import 'dart:ffi';

import 'package:flutter_jscore/flutter_jscore.dart';

Map contextMap = new Map();
String basicJS = '''
var __page__ ;
var Page = function(content){
    // 就收上下文，处理到全局
    __page__ = content;
    __page__.data.list = [{
            "rating":{
                "max":10,
                "numRaters":2204,
                "average":"9.1",
                "min":0
            },
            "subtitle":"从入门到实践",
            "author":[
                "[美] 埃里克·马瑟斯"
            ],
            "pubdate":"2016-7-1",
            "tags":[
                {
                    "count":2477,
                    "name":"Python",
                    "title":"Python"
                },
                {
                    "count":1500,
                    "name":"编程",
                    "title":"编程"
                },
                {
                    "count":1136,
                    "name":"python",
                    "title":"python"
                },
                {
                    "count":1010,
                    "name":"入门",
                    "title":"入门"
                },
                {
                    "count":926,
                    "name":"计算机",
                    "title":"计算机"
                },
                {
                    "count":452,
                    "name":"软件开发",
                    "title":"软件开发"
                },
                {
                    "count":420,
                    "name":"IT",
                    "title":"IT"
                },
                {
                    "count":362,
                    "name":"Programming",
                    "title":"Programming"
                }
            ],
            "origin_title":"Python Crash Course",
            "image":"https://img3.doubanio.com/view/subject/m/public/s28891775.jpg",
            "binding":"平装",
            "translator":[
                "袁国忠"
            ],
            "catalog":"第一部分　基础知识460",
            "ebook_url":"https://read.douban.com/ebook/122163837/",
            "pages":"459",
            "images":{
                "small":"https://img3.doubanio.com/view/subject/s/public/s28891775.jpg",
                "large":"https://img3.doubanio.com/view/subject/l/public/s28891775.jpg",
                "medium":"https://img3.doubanio.com/view/subject/m/public/s28891775.jpg"
            },
            "alt":"https://book.douban.com/subject/26829016/",
            "id":"26829016",
            "publisher":"人民邮电出版社",
            "isbn10":"7115428026",
            "isbn13":"9787115428028",
            "title":"Python编程",
            "url":"https://api.douban.com/v2/book/26829016",
            "alt_title":"Python Crash Course",
            "author_intro":"作者简介：",
            "ebook_price":"44.50",
            "series":{
                "id":"660",
                "title":"图灵程序设计丛书"
            },
            "price":"89.00元"
        }]
};

// 获取flutter需要的表达式
var getTarget = function(expression){

    expression = expression.replace('return',''); //eval内不能执行return
    with(__page__.data){
        return eval(expression);
    };

}   ;
// 求取条件表达式
var exec = function(type,expression){
    const value = getTarget(expression);
    switch(type){
        case 'handleRepeat':  return value.length || 0;
        default : return value;
    }
};
//
var event = function(func, params){
    with(__page__){
        var res = eval(func+"("+JSON.stringify(params)+")");
        return res;
    };

}

''';
/***
 * 获取JsContext对象
 */
JSContext getJscoreContext(String type){
    if(contextMap[type] == null){
        JSContext  jsContext = JSContext.createInGroup();
        jsContext.evaluate(basicJS);
        // JSValue jsValue = jsContext.evaluate('__page__');
        // print(jsValue.string);
        contextMap[type] = jsContext;
    }
    return contextMap[type];
}





